<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <style>
        .table {
            width: 800px;
            max-width: 800px;
            margin: auto;
        }

        h1 {
            text-align: center;
            margin: 30px 0px;
        }

        img {
            width: 80px;
            height: 80px;
        }
    </style>
</head>

<body>
    <h1>商品信息</h1>
    <table width="600" class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>商品名</th>
                <th>图片信息</th>
                <th>单价</th>
                <th>库存</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

</body>

</html>
<script src="https://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js"></script>
<script>


    // 原始的传参方式，必须考虑顺序，必须一一对应，如果想要省略，只能省略最后的，不能省略前面
   /*  function ajax( type ,url,data ) {
        return new Promise((resolve, reject) => {
            $.ajax({
                type: type,
                url: url,
                data:data,
                success(data) {
                    resolve([null, data])
                },
                error(err) {
                    resolve([err, undefined])
                }
            })
        })
    } */


    /* function ajax( {type,url,data} ) {
        // console.log(type,url,data);

        return new Promise((resolve, reject) => {
            $.ajax({
                type: type,
                url: url,
                data:data,
                success(data) {
                    resolve([null, data])
                },
                error(err) {
                    resolve([err, undefined])
                }
            })
        })
    } */


    function ajax({ type="get",url="http://localhost:3000/goodslist",data={} } ={}) {

        return new Promise((resolve, reject) => {
            $.ajax({
                type,
                url,
                data,
                success(data) {
                    resolve([null, data])
                },
                error(err) {
                    resolve([err, undefined])
                }
            })
        })
    }

    async function render() {
        // let [err, data] = await p
        // let [err, data] = await ajax("get","http://localhost:3000/goodslist")
        // let [err, data] = await ajax("get","http://localhost:3000/goodslist",{username:"zs"})
        // let [err, data] = await ajax("get","http://localhost:3000/goodslist",{username:"zs"})
        // 一些参数，有的时候不用传，以上的传参形式，需要考虑顺序问题

        //  let [err, data] = await ajax({
        //     // type:"get",
        //     url:"http://localhost:3000/goodslist",
        //     // data:{username:"zs"}
        //  })

        // let [err, data] = await ajax({ })
        let [err, data] = await ajax()

         console.log([err, data]);
        

    }

    render()


    let { type="get",url="http://localhost:3000/goodslist",data={} }={}

</script>